
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<span style="font-size:18px;">
<html>
<head>
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link rel="stylesheet" type="text/css" href="attendance/css/bootstrap.min.css" />
    <link rel="stylesheet" type="text/css" href="attendance/css/commonly-used-show.css" />
    <script type="text/javascript" src="attendance/js/jquery.min.js"></script>
    <script type="text/javascript" src="attendance/js/bootstrap.js" ></script>

    <title>请假申请</title>

</head>

<body class="mybody">


<jsp:include page="theTopMenuUser.jsp" />


<div class="container-fluid">
    <div class="row">
        <div class="col-md-2">
            <div class="text-center" id="left-content">
                <div class="list-group">
                    <a href="${pageContext.request.contextPath}/user" class="list-group-item active">首页</a>
                </div>
                <div class="list-group">
                    <a href="${pageContext.request.contextPath}/clockInClass" class="list-group-item">打卡签到</a>
                    <a href="${pageContext.request.contextPath}/attendanceRecord" class="list-group-item">考勤记录</a>
                    <a href="${pageContext.request.contextPath}/requestForAbsence" class="list-group-item">请假申请</a>
                    <a href="${pageContext.request.contextPath}/askForLeaveToSee" class="list-group-item">请假查看</a>

                </div>
            </div>
        </div>



        <div class="col-md-10">
            <div class="col-md-12">
                    <div class="panel panel-primary">
                        <div class="panel-body">
                            <div class="alert alert-success">

                                 <div>
                                     <h3 style="text-align: center">请假申请</h3>
                                     <hr style="filter: alpha(opacity=100,finishopacity=0,style=3)" width="100%" color="#6f5999" size="3"/>

                                     <div id="personal_user" style="margin-left: 150px; margin-right: 10px;">
                                         <div class="form-group same-line">
                                             <label><b style="color: red">*&nbsp</b>姓名：</label>
                                             <input type="text" id="user_name" class="form-control edit" placeholder="请输入姓名">
                                         </div>

                                         <div class="form-group same-line-left">
                                             <label ><b style="color: red">*&nbsp</b>请假类型：</label>
                                             <select class="form-control edit" title="请选择" id="leave_type" data-live-search="true" data-size="5">
                                                 <option value="" disabled selected hidden>请选择</option>
                                                 <option value="0">病假</option>
                                                 <option value="1">事假</option>
                                                 <option value="2">其他</option>
                                             </select>
                                         </div>



                                         <div class="form-group same-line">
                                             <label ><b style="color: red">*&nbsp</b>开始时间：</label>
                                             <input type="date" id="start_time" class="form-control edit" placeholder="请选择" >
                                         </div>

                                         <div class="form-group same-line-left">
                                             <label for="end_time"><b style="color: red">*&nbsp</b>结束时间：</label>
                                             <input type="date" id="end_time" class="form-control edit" placeholder="请选择" >
                                         </div>

                                         <div class="form-group same-line">
                                             <label ><b style="color: red">*&nbsp</b>请假原因：</label>
                                             <textarea id="reason_leave" style="height: 150px" class="form-control edit" placeholder="请输入请假原因" ></textarea>
                                         </div>

                                     </div>

                                     <br>

                                     <div class="text-center" >
                                         <div class="form-group same-line-clear">
                                             <input type="button" value="提交" class="btn btn-primary" onclick="present()">
                                             <input type="reset" value="重置" class="btn btn-danger interval" onclick="reset()">
                                         </div>

                                         <div>
                                             <label id="user_null" style="color: red"></label>
                                         </div>
                                     </div>

                                 </div>

                            </div>
                        </div>
                    </div>
                </div>
        </div>



    </div>

</div>


<script type="text/javascript">
    var userdt = JSON.parse(sessionStorage.getItem("user"));
    var coursely = JSON.parse(sessionStorage.getItem("course"));
    var userNull = document.getElementById("user_null");
    var name = "";
    if (userdt.result.userName != null){
        name = userdt.result.userName;
        $("#usname").text(userdt.result.userName);
    }else {
        name = userdt.result.userLoginName;
        $("#usname").text(userdt.result.userLoginName);
    }

    var startTime;
    var endTime;

    function present(){
        var userName = $("#user_name").val();
        var leaveType = $("#leave_type").val();
        var reasonLeave = $("#reason_leave").val();
        startTime = $("#start_time").val();
        endTime = $("#end_time").val();

        if ($.isEmptyObject(userName)){
            userNull.innerHTML = "姓名不能为空";
            return false;
        }else if ($.isEmptyObject(leaveType)){
            userNull.innerHTML = "请假类型不能为空";
            return false;
        }else if ($.isEmptyObject(reasonLeave)){
            userNull.innerHTML = "请假原因不能为空";
            return false;
        }else if ($.isEmptyObject(startTime)){
            userNull.innerHTML = "请假开始时间不能为空";
            return false;
        }else if ($.isEmptyObject(endTime)){
            userNull.innerHTML = "请假结束时间不能为空";
            return false;
        }

        var postUrl = "${pageContext.request.contextPath}/member/insertSelective";
        var str = {
            "userId": userdt.result.id,
            "courseId": coursely.result.id,
            "userName": userName,
            "leaveType": leaveType,
            "reasonLeave": reasonLeave,
            "startTime": startTime,
            "endTime": endTime
        };

        $.ajax({
            type:"post",
            url:postUrl,
            headers: {
                "Content-Type":"application/json"
            },
            data:JSON.stringify(str),
            contentType: "application/json; charset=utf-8",
            dataType:"json",
            success:function(data){
                if (data.status == "0"){
                    userNull.innerHTML = data.msg;
                }else {
                    vacate();
                }

            },
            error:function(xhr,state,errorThrown){
                //alert("发生网络错误，错误码为：" + xhr.status)
                location.href = "${pageContext.request.contextPath}/error";
            }
        });

    }


    function vacate(){
        var postUrl = "${pageContext.request.contextPath}/member/clockInsert";
        var str = {
            "userId": userdt.result.id,
            "userName": name,
            "startAttendDate": startTime,
            "startAttendState": "4",
            "courseId": coursely.result.id,
            "startAttendTime": "00:00:00",
            "endAttendDate": endTime,
            "endAttendState": "3",
            "endAttendTime": "00:00:00"
        };

        $.ajax({
            type:"post",
            url:postUrl,
            headers: {
                "Content-Type":"application/json"
            },
            data:JSON.stringify(str),
            contentType: "application/json; charset=utf-8",
            dataType:"json",
            success:function(data){
                if (data.status == "0"){
                    userNull.innerHTML = data.msg;
                }else {
                    location.href = "${pageContext.request.contextPath}/askForLeaveToSee";
                }

            },
            error:function(xhr,state,errorThrown){
                //alert("发生网络错误，错误码为：" + xhr.status)
                location.href = "${pageContext.request.contextPath}/error";
            }
        });
    }

</script>

</body>
</html>
</span>